﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using sWarranty;

namespace LCUV.UC
{
    public partial class Header : System.Web.UI.UserControl
    {
        public int _currentUserID = 0;
        public string _currentUserName = "";
        public string _currentUserRegion = "";
        public int tsdf = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            {// Check Login
                _currentUserID = GetLoginUserID();
                if (_currentUserID == 0)
                    Response.Redirect("Login.aspx");
                //else
                //{
                //    if (Session["UrltoFw"] != null)
                //        if (!string.IsNullOrEmpty(Session["UrltoFw"].ToString()))
                //            Response.Redirect(Session["UrltoFw"].ToString());
                //    //Response.Redirect("Home1.aspx");
                //}
            }// End check Login
            lblUser.Text = _currentUserName + ", " + _currentUserRegion;
            if (!IsPostBack)
            {
                DBAction.con = new Oracle.DataAccess.Client.OracleConnection(DBAction.cnn);
                DBAction.con.Open();


                int ParaForMenu = 1;//menu nay cu chuoi, fai co' bien co' dinh de tang
                litMenu.Text = "";
                litSubMenu.Text = "";
                Entities db = new Entities();
                var L = from t in db.TB_USER_LEVEL
                        where t.USER_ID == _currentUserID
                        select t;
                int _level = L.Single().LEVEL_ID;
                var m = from lm in db.TB_LEVEL_MENU
                        from M in db.TB_MENU
                        where lm.LEVEL_ID == _level && M.ACTIVE == 1 && M.ID == lm.MENU_ID
                        orderby M.DISPLAY_ORDER ascending
                        select lm;
                foreach (var item in m)
                {

                    int PARENT_MENU_ID = Convert.ToInt32(item.MENU_ID);
                    litMenu.Text += "<a href='" + DBAction.GetMenuCode(item.MENU_ID) + "'" + "' target='" + DBAction.GetMenuOpenType(item.MENU_ID) + "'>" + DBAction.GetMenuName(item.MENU_ID) + "</a>";
                    litSubMenu.Text += "<div id='submenu_" + ParaForMenu + "'>";
                    var subM = from t in db.TB_SUB_MENU
                               where t.PARENT_MENU_ID == PARENT_MENU_ID && t.ACTIVE == 1
                               select t;
                    foreach (var subItem in subM)
                    {
                        if (!string.IsNullOrEmpty(DBAction.GetSubMenuOpenType(subItem.ID)))
                            litSubMenu.Text += " <a href='#' onclick=\"openPopUp('" + DBAction.GetSubMenuCode(subItem.ID) + "','" + DBAction.GetSubMenuOpenType(subItem.ID) + "')\">" + subItem.NAME + "</a>";
                        else
                            litSubMenu.Text += " <a href='" + DBAction.GetSubMenuCode(subItem.ID) + "'" + " >" + subItem.NAME + "</a>";
                    }
                    litSubMenu.Text += "</div>";
                    ParaForMenu++;
                }
            }
            tsdf = 1;

            
        }

        protected int GetLoginUserID()
        {
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    _currentUserName = u.Single().DISPLAY_NAME;
                    _currentUserID = u.Single().ID;
                    _currentUserRegion = u.Single().REGION_ID;
                    return _currentUserID;
                }
                else
                    return 0;
            }
            catch (Exception)
            {
                return 0;
            }

        }

        public string ttt()
        {
            int dem = 1;
        xxx:
            if (tsdf == 1)
                return "<div id='galleria'><a href='../images/2.JPG'><img src='../images/2.JPG' /></a> <a href='../images/3.JPG'><img src='../images/3.JPG' /></a> <a href='../images/4.JPG'><img src='../images/4.JPG' /></a></div>";
            else
            {
                dem++;
                if (dem < 10)
                    goto xxx;
                else
                    return "<div id='galleria'><a href='../images/2.JPG'><img src='../images/2.JPG' /></a> <a href='../images/3.JPG'><img src='../images/3.JPG' /></a> <a href='../images/4.JPG'><img src='../images/4.JPG' /></a></div>";

            }
        }

    }
}